﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Telerik.Web.UI;

namespace TCV_WEB.Admin.Page
{
    public partial class AnalysisDefinition : System.Web.UI.Page
    {
        string sErr = "";
        string idHandler = "218";
        string idHnew = "219";
        string idHedit = "220";
        string idHdelete = "221";
        string idHtransferin = "222";
        string idHtransferout = "223";
        string xmlConfiguration = "PMSREAL.COMMONSETUP.ANALYSISDEFINITION.xml";
        string _db = "EXA";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
            }
        }



        private DTO.ANDInfo GetDataFromForm()
        {
            DTO.ANDInfo result = new DTO.ANDInfo();
            result.CATEGORY = txtCategory.Text;
            if (chkAllowAmend.Checked) result.AMENDABLE = "Y";
            else result.AMENDABLE = "N";
            result.DESCRIPTION = txtDescription.Text;
            //result.LOOKUP 
            result.MASK = txtMask.Text;
            if (chkNonValidated.Checked) result.NONVALIDATED = "Y";
            else result.NONVALIDATED = "N";
            result.SHORTDESC = txtShortDescription.Text;
            result.SUBCATEGORY = txtSubCategory.Text;
            result.SUN_DB = _db;
            result.UPDATED = "";
            return result;
        }

        private void SetDataToForm(DTO.ANDInfo objEntr)
        {
            txtCategory.Text = objEntr.CATEGORY;
            chkAllowAmend.Checked = objEntr.AMENDABLE == "Y";

            txtDescription.Text = objEntr.DESCRIPTION;
            txtMask.Text = objEntr.MASK;
            chkNonValidated.Checked = objEntr.NONVALIDATED == "Y";

            txtShortDescription.Text = objEntr.SHORTDESC;
            txtSubCategory.Text = objEntr.SUBCATEGORY;
            //result.UPDATED = "";
        }

        private void EnableForm()
        {
            txtSubCategory.Enabled = true;
            txtShortDescription.Enabled = true;
            txtMask.Enabled = true;
            txtDescription.Enabled = true;
            txtCategory.Enabled = true;
            chkAllowAmend.Enabled = true;
            chkNonValidated.Enabled = true;
        }



        protected void btnNew_Click(object sender, ImageClickEventArgs e)
        {
            RefreshForm();
            EnableForm();
        }

        //private void DisableForm()
        //{
        //    txtSubCategory.Enabled = false;
        //    txtShortDescription.Enabled = false;
        //    txtMask.Enabled = false;
        //    txtDescription.Enabled = false;
        //    txtCategory.Enabled = false;
        //    chkAllowAmend.Enabled = false;
        //    chkNonValidated.Enabled = false;
        //}
        private void RefreshForm()
        {
            txtSubCategory.Text = "";
            txtShortDescription.Text = "";
            txtMask.Text = "";
            txtDescription.Text = "";
            txtCategory.Text = "";
            chkAllowAmend.Checked = false;
            chkNonValidated.Checked = false;
            lbErr.Text = "";
        }

        protected void btnReload_Click(object sender, EventArgs e)
        {
            if (Session["dataTransfer"] != null)
            {
                Session["dataTransfer"] = null;
                return;
            }
        }

        protected void dgvList_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            string sErr = "";
            BUS.ANDControl contractCtrl = new BUS.ANDControl();
            DataTable dt = contractCtrl.GetAll(_db, ref sErr);
            dgvList.DataSource = dt;
        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            RefreshForm();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            DTO.ANDInfo info = GetDataFromForm();
            BUS.ANDControl control = new BUS.ANDControl();
            if (!control.IsExist(info.SUN_DB, info.CATEGORY, info.SUBCATEGORY))
            {
                sErr = control.Update(info);
                if (sErr != "")
                    lbErr.Text = sErr;
                else lbErr.Text = "Update successfull";
            }
            else
            {
                control.Add(info, ref sErr).ToString();
                if (sErr != "")
                    lbErr.Text = sErr;
                else lbErr.Text = "Insert successfull";
            }
            if (sErr == "")
            {
                dgvList.Rebind();
            }
            else
            { }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            BUS.ANDControl control = new BUS.ANDControl();
            foreach (GridItem item in dgvList.SelectedItems)
            {
                if (item.ItemIndex >= 0)
                {
                    // SUN_DB,CATEGORY,SUBCATEGORY
                    string db = item.OwnerTableView.DataKeyValues[item.ItemIndex]["SUN_DB"].ToString();
                    string cate = item.OwnerTableView.DataKeyValues[item.ItemIndex]["CATEGORY"].ToString();
                    string sub = item.OwnerTableView.DataKeyValues[item.ItemIndex]["SUBCATEGORY"].ToString();
                    if (control.IsExist(db, cate, sub))
                        sErr = control.Delete(db, cate, sub);
                }
            }
            dgvList.Rebind();
        }

        protected void dgvList_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName.ToUpper() == "SELECTED")
            {
                string db = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["SUN_DB"].ToString();
                string cate = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["CATEGORY"].ToString();
                string sub = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["SUBCATEGORY"].ToString();
                BUS.ANDControl ctr = new BUS.ANDControl();
                SetDataToForm(ctr.Get(_db, cate, sub, ref sErr));
                lbErr.Text = "";
            }
        }


    }
}
